package net.wesc.ac.server;

/**
 * solve for GS Equation
 * lg(lambda)=-A*|z+z-|*sqrt(I)/(1+B*a*sqrt(I)) + 2*Va/(Va+Vc)*Va*m*a0 + 2*Vc/(Va+Vc)*Vc*m*a1
 * 对参数a0求偏倒数后2*Va/(Va+Vc)*Va*m;
 * 对参数a1求偏倒数后2*Vc/(Va+Vc)*Vc*m;
 * A与温度的关系为A正比于T的-3/2次方
 */
public class LMGS implements LMfunc
{  
  static final int a0 = 0; //beta0
  static final int a1 = 1; //beta1
  static final double A0 = 0.509;
  static final double temp0 = 298.15;
  static int Zc;
  static int Za;
  static int Va;
  static int Vc;
  static int V;
  static double temp = temp0;
  static double A = A0;
  
  public LMGS(int tZc, int tZa, double ttemp){
	  Zc = tZc;
	  Za = tZa;
	  Vc = Za;
	  Va = Zc;
	  V = Va+Vc;
	  temp = ttemp;
	  A = A0*Math.pow(temp0/temp, 1.5);
  }
  
  public double[] initial()
  {
    double[] a = new double[2];
    a[a0] = 1;
    a[a1] = 1;
    return a;
  } //initial

  public double val(double[] x, double[] a){
	  double I = 0.5*(Va*x[0]*Math.pow(Za, 2)+Vc*x[0]*Math.pow(Zc, 2)); //I ionic strength
      return (-A*Math.abs(Za*Zc)*Math.sqrt(I))/(1+Math.sqrt(I)) + 2*Va/(Va+Vc)*a[a0]*Va*x[0] + 2*Vc/(Va+Vc)*a[a1]*Vc*x[0];
  } //val
  
  public double grad(double[] x, double[] a, int a_k){
	  if (a_k == a0){
		  return 2*Va/(Va+Vc)*Va*x[0];
	  }
	  if (a_k == a1){
		  return 2*Vc/(Va+Vc)*Vc*x[0];
	  }
	  else {
		  assert false;
		  return 0.;
	  }
  } //grad
} //DHTest

